package de.dailab.graphworld.world;

import java.util.ArrayList;

import de.dailab.graphworld.ontology.Edge;
import de.dailab.graphworld.ontology.Node;
import de.dailab.graphworld.pathfinding.AStar;
import de.dailab.jiactng.agentcore.action.AbstractMethodExposingBean;
import de.dailab.jiactng.agentcore.action.scope.ActionScope;

public class PathfinderAgentBean extends AbstractMethodExposingBean {
	/** Calculates the shortest path between the input nodes using A*. Inputs: Source Node, Target Node; Returns: List of Edges*/
	public static final String ACTION_GETPATH = "Graphworld.Pathfinding.getPath";
	
	@Expose(name=ACTION_GETPATH, scope=ActionScope.GLOBAL)
	public ArrayList<Edge> getPath(Node source, Node target) {
		Node sourceFromMemory = memory.read(new Node(source.getID()));
		Node targetFromMemory = memory.read(new Node(target.getID()));
		
		ArrayList<Edge> path = AStar.findPath(sourceFromMemory, targetFromMemory);
		
		ArrayList<Edge> returnedPath = new ArrayList<Edge>();
		for (Edge e : path) {
			returnedPath.add(new Edge(e.getWeight(), new Node(e.getTarget().getID())));
		}
		return returnedPath;
	}
}
